#include <stdio.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/pem.h>

static uint8_t g_crlDerData04[] = {
    0x30, 0x82, 0x02, 0x13, 0x30, 0x81, 0xfc, 0x02, 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
    0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x74, 0x31, 0x0b, 0x30, 0x09, 0x06,
    0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x43, 0x4e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04,
    0x08, 0x0c, 0x07, 0x4a, 0x69, 0x61, 0x6e, 0x67, 0x53, 0x75, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03,
    0x55, 0x04, 0x07, 0x0c, 0x07, 0x4e, 0x61, 0x6e, 0x4a, 0x69, 0x6e, 0x67, 0x31, 0x0a, 0x30, 0x08,
    0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x01, 0x74, 0x31, 0x0a, 0x30, 0x08, 0x06, 0x03, 0x55, 0x04,
    0x0b, 0x0c, 0x01, 0x74, 0x31, 0x0c, 0x30, 0x0a, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x03, 0x7a,
    0x68, 0x62, 0x31, 0x1b, 0x30, 0x19, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09,
    0x01, 0x16, 0x0c, 0x74, 0x65, 0x73, 0x74, 0x40, 0x31, 0x32, 0x33, 0x2e, 0x63, 0x6f, 0x6d, 0x17,
    0x0d, 0x32, 0x33, 0x31, 0x31, 0x30, 0x37, 0x30, 0x32, 0x35, 0x32, 0x30, 0x37, 0x5a, 0x17, 0x0d,
    0x32, 0x33, 0x31, 0x32, 0x30, 0x37, 0x30, 0x32, 0x35, 0x32, 0x30, 0x37, 0x5a, 0x30, 0x2c, 0x30,
    0x2a, 0x02, 0x19, 0x00, 0xff, 0x01, 0xff, 0x01, 0xff, 0x01, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x17, 0x0d, 0x32, 0x33,
    0x31, 0x31, 0x30, 0x37, 0x30, 0x32, 0x35, 0x31, 0x30, 0x33, 0x5a, 0xa0, 0x26, 0x30, 0x24, 0x30,
    0x22, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04, 0x1b, 0x02, 0x19, 0x00, 0xff, 0x01, 0xff, 0x01, 0xff,
    0x01, 0xff, 0x01, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    0xff, 0xff, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b,
    0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x70, 0x1d, 0xb7, 0x96, 0x4a, 0xd1, 0x62, 0x8e, 0x3c,
    0x61, 0x3e, 0x51, 0x89, 0x3c, 0x48, 0xca, 0x34, 0x8a, 0xd6, 0x44, 0xd3, 0xf9, 0x9c, 0x5c, 0xde,
    0x82, 0x7a, 0x81, 0x77, 0x0f, 0xa1, 0x2d, 0x63, 0xef, 0xf4, 0xdd, 0xf9, 0xf0, 0xe6, 0x15, 0xf8,
    0xcb, 0x39, 0x47, 0x8a, 0x59, 0x8a, 0xeb, 0xc7, 0xf4, 0xfb, 0x37, 0x27, 0xe3, 0x17, 0x18, 0xe7,
    0x3a, 0x7d, 0xfb, 0x44, 0xa9, 0x90, 0x08, 0x79, 0x55, 0x26, 0xda, 0x80, 0xc4, 0x8b, 0x2d, 0x9d,
    0x70, 0xb0, 0xa1, 0x2c, 0xea, 0xeb, 0xc9, 0x2a, 0x2b, 0xdb, 0x9c, 0xcd, 0x90, 0x9a, 0x63, 0x35,
    0xab, 0xf9, 0xa3, 0xf3, 0xfe, 0x86, 0xd0, 0x47, 0x11, 0x56, 0x46, 0xcd, 0x1a, 0x27, 0x2d, 0xb1,
    0x2a, 0xf5, 0xe9, 0xea, 0x20, 0x4e, 0x6d, 0x79, 0x40, 0x41, 0x54, 0x87, 0x5e, 0x42, 0x39, 0x8a,
    0x48, 0xe4, 0xc5, 0x1e, 0x57, 0xa5, 0xf8, 0xbe, 0x54, 0x7d, 0x28, 0x8d, 0x38, 0xb9, 0x36, 0x3a,
    0x4f, 0xb2, 0xc2, 0xc9, 0x30, 0xde, 0x1b, 0xee, 0xa0, 0x99, 0xff, 0x7d, 0xf8, 0xaa, 0x46, 0x7b,
    0x78, 0x7e, 0x8e, 0xdf, 0x9e, 0x51, 0xdb, 0x3d, 0x17, 0x33, 0x8b, 0xf1, 0x40, 0x90, 0xc0, 0x24,
    0x1a, 0x05, 0xe5, 0x7f, 0x44, 0x15, 0x81, 0x49, 0x73, 0xfb, 0xb0, 0x75, 0x93, 0xc8, 0x4e, 0x74,
    0x8a, 0x68, 0x6e, 0x48, 0x04, 0xcb, 0x75, 0x93, 0x97, 0xa2, 0xe6, 0x33, 0x8b, 0xb0, 0xc0, 0xda,
    0xb5, 0x04, 0x81, 0xac, 0xfb, 0x1c, 0x84, 0x14, 0x41, 0x9a, 0x0d, 0xa4, 0x04, 0x0c, 0xfc, 0x66,
    0xae, 0xa3, 0x81, 0x6d, 0xa3, 0xe5, 0x46, 0x90, 0x67, 0xa7, 0xb4, 0xfe, 0x82, 0x8e, 0x8a, 0x25,
    0xc1, 0xfe, 0xe0, 0x0f, 0xc2, 0x4a, 0x41, 0x2c, 0x01, 0x9e, 0xc2, 0x29, 0xe4, 0x79, 0x92, 0x89,
    0x54, 0x18, 0x17, 0x39, 0x16, 0x00, 0x16,
};

static uint8_t g_crlDerData[] = {
    0x30, 0x82, 0x01, 0xE3, 0x30, 0x81, 0xCC, 0x02, 0x01, 0x01, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86,
    0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x30, 0x2C, 0x31, 0x0B, 0x30, 0x09, 0x06,
    0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x43, 0x4E, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55, 0x04,
    0x0A, 0x0C, 0x04, 0x74, 0x65, 0x73, 0x74, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04, 0x03,
    0x0C, 0x05, 0x73, 0x75, 0x62, 0x63, 0x61, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x39, 0x31, 0x32, 0x30,
    0x36, 0x34, 0x37, 0x35, 0x30, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x31, 0x30, 0x31, 0x32, 0x30, 0x36,
    0x34, 0x37, 0x35, 0x30, 0x5A, 0x30, 0x3B, 0x30, 0x13, 0x02, 0x02, 0x03, 0xE8, 0x17, 0x0D, 0x32,
    0x33, 0x30, 0x39, 0x31, 0x32, 0x30, 0x36, 0x34, 0x37, 0x34, 0x39, 0x5A, 0x30, 0x24, 0x02, 0x13,
    0x17, 0x5D, 0x6A, 0x9F, 0xEC, 0xA9, 0x09, 0xD7, 0x12, 0xB2, 0x48, 0x52, 0xA6, 0x3E, 0x48, 0xF6,
    0x12, 0x93, 0xA9, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x39, 0x31, 0x32, 0x30, 0x36, 0x34, 0x32, 0x35,
    0x34, 0x5A, 0xA0, 0x2F, 0x30, 0x2D, 0x30, 0x1F, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04, 0x18, 0x30,
    0x16, 0x80, 0x14, 0x22, 0x2B, 0x18, 0xDE, 0x84, 0xD9, 0x30, 0x4E, 0xF6, 0x3D, 0xDF, 0xD7, 0xF4,
    0x02, 0xED, 0x44, 0x22, 0x84, 0x20, 0xE8, 0x30, 0x0A, 0x06, 0x03, 0x55, 0x1D, 0x14, 0x04, 0x03,
    0x02, 0x01, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
    0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x40, 0xA1, 0x82, 0x5E, 0xCE, 0x5A, 0x5D, 0x8E, 0x7A,
    0xD3, 0xA0, 0x3F, 0xD0, 0x7C, 0xA7, 0x2E, 0x6A, 0xBE, 0x7E, 0xB2, 0x7B, 0xA7, 0x95, 0x23, 0xF5,
    0xC0, 0xF6, 0xBF, 0x06, 0xD9, 0x57, 0x8C, 0x5A, 0x3F, 0x61, 0x39, 0x8D, 0x7A, 0x20, 0x07, 0x3E,
    0xD8, 0x0A, 0x39, 0xB1, 0xA7, 0x43, 0xC1, 0xF7, 0xDE, 0x57, 0x0B, 0xDA, 0x22, 0xDD, 0x02, 0x90,
    0x80, 0xB5, 0x4A, 0x63, 0x83, 0x73, 0xDB, 0x55, 0x90, 0x45, 0xE7, 0x26, 0x99, 0x99, 0xB5, 0x70,
    0x3C, 0x1E, 0x0C, 0x33, 0xF0, 0x18, 0x9F, 0x3F, 0x23, 0x47, 0x76, 0x0B, 0x03, 0x13, 0x25, 0xF3,
    0xFB, 0xAC, 0x48, 0x2C, 0xBA, 0x18, 0x08, 0x06, 0xAF, 0x89, 0x52, 0x31, 0x5C, 0x34, 0xD6, 0x96,
    0x76, 0x26, 0xB6, 0x1A, 0xEF, 0xDA, 0x02, 0xE2, 0x23, 0x95, 0xA2, 0x79, 0x03, 0x85, 0xBB, 0xBE,
    0xF8, 0x46, 0x55, 0x4C, 0x7D, 0x08, 0x35, 0x1D, 0x37, 0xC6, 0x05, 0xE6, 0x49, 0xC0, 0xDC, 0x1A,
    0x10, 0xD6, 0xE1, 0x5C, 0xD7, 0x3E, 0xE0, 0x35, 0xC9, 0x24, 0x22, 0x94, 0xD3, 0x71, 0xC4, 0x0B,
    0xCE, 0x81, 0x0B, 0x14, 0x31, 0xBC, 0xFF, 0x3F, 0x9B, 0x3A, 0x70, 0x2E, 0x4A, 0x0D, 0x65, 0x64,
    0x2C, 0xAF, 0xF5, 0xF3, 0xB3, 0xF6, 0x55, 0x5A, 0x7C, 0x2F, 0xAA, 0x68, 0x7A, 0x3E, 0x35, 0x6F,
    0x6B, 0x74, 0x28, 0x71, 0x57, 0x5A, 0x02, 0xE5, 0x2E, 0xA3, 0x1D, 0x3B, 0xBC, 0xEF, 0xD0, 0x8E,
    0x31, 0x09, 0xCB, 0xAF, 0x78, 0xC4, 0x04, 0x2B, 0x33, 0x37, 0x30, 0xE3, 0x14, 0x8E, 0xB8, 0x97,
    0xA7, 0xF7, 0x25, 0x59, 0xCB, 0x65, 0xEA, 0x69, 0xE8, 0xD5, 0x35, 0x81, 0xDB, 0xA7, 0x8D, 0x02,
    0xF8, 0x82, 0xD5, 0x90, 0x5D, 0x91, 0x65, 0x15, 0x5E, 0xF9, 0xC5, 0x1F, 0x1A, 0x6A, 0x17, 0x7C,
    0xB1, 0xE0, 0x79, 0xA0, 0x34, 0xCF, 0x42
};

int main(int argc, char *argv[]) {
    BIO *bio = BIO_new_mem_buf(g_crlDerData, sizeof(g_crlDerData));
    if (bio == NULL) {
        printf("bio get null!");
        return 1;
    }
    X509_CRL *crlOut = NULL;
    crlOut = d2i_X509_CRL_bio(bio, NULL);

    int count = X509_CRL_get_ext_count(crlOut);
    printf("count = %d, at line %d.\n", count, __LINE__);

    // // 获取第一个扩展项
    // X509_EXTENSION * pExtFound = X509_CRL_get_ext_by_count(crlOut, 0);
    // if (pExtFound) {
    //     // 输出扩展项的名称和数据
    //     printf("扩展项名称：%s\n", OBJ_nid2sn(OBJ_obj2nid(X509_EXTENSION_get_object(pExtFound))));
    //     printf("扩展项数据：%s\n", (char *)X509_EXTENSION_get_data(pExtFound)->data);
    // } else {
    //     printf("CRL 中没有扩展项\n");
    // }

    // 查找要删除的扩展
    int ext_index = X509_CRL_get_ext_by_NID(crlOut, NID_crl_number, -1);
    printf("ext_index=%d, at line %d.\n", ext_index, __LINE__);
    if (ext_index < 0) {
        printf("not found,at line %d!\n", __LINE__);
        return 1;
    }

    // 删除扩展
    X509_CRL_delete_ext(crlOut, ext_index);
    // int crit = X509_EXTENSION_get_critical(ext);
    // if (crit) {
    //     printf("Extension with NID %d is critical\n", ext_index);
    // } else {
    //     printf("Extension with NID %d is not critical,crit=%d\n", ext_index, crit);
    // }

    // 释放扩展项内存
    // X509_EXTENSION_free(ext);

    uint8_t *out = NULL;
    int32_t length = i2d_X509_CRL(crlOut, &out);
    if (length <= 0) {
        printf("Do i2d_X509_CRL fail!");
        return -1;
    }
    for(int index=0; index < length; index++)
    {
        if(index%16 == 0)
        {
            printf("\n");
        }
        printf("0x%02x, ", out[index]);   
    }

    return 0;
}
